package cn.j0n4than.api.utils;

import cn.j0n4than.api.config.ResponseConfig;
import lombok.Data;

import java.util.HashMap;

/**
 * JSON返回
 *
 * @author jonathan
 */
@Data
public class Response {

    private int status;
    private String message;
    private HashMap<String, Object> result = new HashMap<String, Object>();

    private Response() {

    }

    /**
     * 成功
     *
     * @return Response
     */
    public static Response success() {
        Response response = new Response();
        response.status = ResponseConfig.SUCCESS;
        response.message = "success";
        return response;
    }

    /**
     * 失败
     *
     * @return Response
     */
    public static Response error() {
        Response response = new Response();
        response.status = ResponseConfig.ERROR;
        response.message = "error";
        return response;
    }

    /**
     * 待定
     *
     * @return Response
     */
    public static Response response() {
        Response response = new Response();
        response.status = 0;
        response.message = null;
        return response;
    }

    /**
     * 设置状态
     *
     * @param status 状态码
     * @return Response
     * @see cn.j0n4than.api.config.ResponseConfig
     */
    public Response setStatus(int status) {
        this.status = status;
        return this;
    }

    /**
     * 设置信息
     *
     * @param message 信息
     * @return Response
     */
    public Response setMessage(String message) {
        this.message = message;
        return this;
    }

    /**
     * 设置结果
     *
     * @param result map
     * @return Response
     */
    public Response setResult(HashMap<String, Object> result) {
        this.result = result;
        return this;
    }

    /**
     * 设置结果
     *
     * @param key    键
     * @param object 值
     * @return Response
     */
    public Response setResult(String key, Object object) {
        this.result.put(key, object);
        return this;
    }

    @Override
    public String toString() {
        return "Response{" +
                "status=" + status +
                ", message='" + message + '\'' +
                ", result=" + result +
                '}';
    }
}
